package com.mjh.zhujianju.service.agency.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.mjh.zhujianju.util.FileUrlAdapter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.livew.framework.base.model.FileEO;
import com.livew.framework.base.service.FileService;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.enums.EStatus;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedBeanUtil;
import com.livew.framework.page.PagedResult;
import com.livew.framework.utils.XTEntityUtils;
import com.livew.framework.utils.XTListUtils;
import com.livew.framework.utils.XTStringUtils;
import com.mjh.zhujianju.dao.agency.RemoveMapper;
import com.mjh.zhujianju.dao.agency.RemoveScheduleMapper;
import com.mjh.zhujianju.enums.BusinessEnum;
import com.mjh.zhujianju.model.agency.RemoveEO;
import com.mjh.zhujianju.model.agency.RemoveScheduleEO;
import com.mjh.zhujianju.model.base.AreasEO;
import com.mjh.zhujianju.model.base.ContentEO;
import com.mjh.zhujianju.service.agency.RemoveService;
import com.mjh.zhujianju.service.base.AreasService;
import com.mjh.zhujianju.service.base.ContentService;
import com.mjh.zhujianju.vo.agency.RemoveVO;

/**
 * @author liuyang
 * @Description: 拆迁
 * @date 2018/8/12 10:34
 */
@Service
public class RemoveServiceImpl extends BaseServiceImpl<RemoveEO> implements RemoveService {

	@Autowired
	private RemoveMapper removeMapper;

	@Autowired
	private FileService fileService;

	@Autowired
	private ContentService contentService;
	
	@Autowired
	private AreasService areasService;

	@Resource
	private RemoveScheduleMapper removeScheduleMapper;

	public RemoveServiceImpl(RemoveMapper removeMapper) {
		super(removeMapper);
	}

	@Override
	public List<RemoveVO> queryRemoveList() {
		List<RemoveVO> list = Lists.newArrayList();
		List<RemoveEO> removeEOList = removeMapper.selectAll();
		if (CollectionUtils.isEmpty(removeEOList)) {
			return list;
		}

		// 组装拆迁户
		removeEOList.stream().forEach(removeEO -> {
			RemoveVO removeVO = new RemoveVO();
			BeanUtils.copyProperties(removeEO, removeVO);
			list.add(removeVO);
		});

		List<Long> caseIds = removeEOList.stream().map(removeEO -> removeEO.getId()).distinct()
				.collect(Collectors.toList());

		// 组装图片
		List<FileEO> fileEOList = fileService.getList(caseIds, RemoveEO.class.getName(), null);
		if (!CollectionUtils.isEmpty(fileEOList)) {
			list.stream().forEach(removeVO -> {
				fileEOList.stream().forEach(fileEO -> {
					if (removeVO.getId().equals(fileEO.getCaseId())) {
						removeVO.setFileAction(FileUrlAdapter.assemblyAction(fileEO.getId()));
					}
				});
			});
		}
		return list;
	}

	@Override
	public RemoveVO queryRemoveInfo(Long id) {
		RemoveVO removeVO = new RemoveVO();
		RemoveEO removeEO = removeMapper.selectByPrimaryKey(id);
		if (removeEO == null) {
			return removeVO;
		}
		BeanUtils.copyProperties(removeEO, removeVO);

		String type = BusinessEnum.TYPE_REMOVE.getCode();
		ContentEO contentEO = contentService.queryContentByTypeAndId(type, removeVO.getId());
		if (contentEO != null) {
			removeVO.setContent(contentEO.getContent());
		}
		return removeVO;
	}

	@Override
	@Transactional
	public void saveInfo(RemoveVO vo) throws XTBusinessException {
		RemoveEO eo = new RemoveEO();
		BeanUtils.copyProperties(vo, eo);
		eo.setStatus(EStatus.active.getCode());
		Map<String, String> map =	areasService.getMap(null,vo.getCityCode(),vo.getCountyCode(),vo.getStreetCode());
		if(map!=null) {
			eo.setCityName(map.get(eo.getCityCode()));
			eo.setCountyName(map.get(eo.getCountyCode()));
			eo.setStreetName(map.get(eo.getStreetCode()));
		}
		if(!XTStringUtils.isEmpty(vo.getRemark2())&&vo.getRemark2().contains(",")) {
			String[] latlon = vo.getRemark2().split(",");
			BigDecimal lat= new BigDecimal(latlon[0]);
			BigDecimal lon= new BigDecimal(latlon[1]);
			eo.setLat(lat);
			eo.setLon(lon);
		}
		this.save(eo);
		if (!XTListUtils.isEmpty(vo.getFileIds())) {
			fileService.updateList(vo.getFileIds(), eo.getId(), RemoveEO.class.getName());
		}
		if (!XTListUtils.isEmpty(vo.getScheduleList())) {
			for (RemoveScheduleEO schedule : vo.getScheduleList()) {
				schedule.setRemoveId(eo.getId());
				schedule.setCreateTime(new Date());
				removeScheduleMapper.insertSelective(schedule);
			}
		}
	}

	@Override
	public void updateInfo(RemoveVO vo) throws XTBusinessException {
		RemoveEO eo = new RemoveEO();
		BeanUtils.copyProperties(vo, eo);
		eo.setStatus(EStatus.active.getCode());
		Map<String, String> map =	areasService.getMap(null,vo.getCityCode(),vo.getCountyCode(),vo.getStreetCode());
		if(map!=null) {
			eo.setCityName(map.get(eo.getCityCode()));
			eo.setCountyName(map.get(eo.getCountyCode()));
			eo.setStreetName(map.get(eo.getStreetCode()));
		}
		if(!XTStringUtils.isEmpty(vo.getRemark2())&&vo.getRemark2().contains(",")) {
			String[] latlon = vo.getRemark2().split(",");
			BigDecimal lat= new BigDecimal(latlon[0]);
			BigDecimal lon= new BigDecimal(latlon[1]);
			eo.setLat(lat);
			eo.setLon(lon);
		}
		this.updateByPrimaryKeySelective(eo);
		if (!XTListUtils.isEmpty(vo.getFileIds())) {
			fileService.updateList(vo.getFileIds(), eo.getId(), RemoveEO.class.getName());
		}
		this.removeScheduleMapper.deleteByPrimaryRemoveId(vo.getId());
		if (!XTListUtils.isEmpty(vo.getScheduleList())) {
			for (RemoveScheduleEO schedule : vo.getScheduleList()) {
				if(schedule.getReportdate()==null) {
					continue;
				}
				schedule.setRemoveId(eo.getId());
				removeScheduleMapper.insertSelective(schedule);
			}
		}
	}

	@Override
	public PagedResult<RemoveVO> getVOPage(RemoveVO vo) {
		Integer pageNo = vo.getPageNo() == null ? 1 : vo.getPageNo();
		Integer pageSize = vo.getPageSize() == null ? 15 : vo.getPageSize();
		PageHelper.startPage(pageNo, pageSize);
		List<RemoveEO> list = removeMapper.selectPagination(vo);
		PagedResult<RemoveEO> page = PagedBeanUtil.toPagedResult(list);
		PagedResult<RemoveVO> result = new PagedResult<>();
		BeanUtils.copyProperties(page, result);
		if (!XTListUtils.isEmpty(list)) {
			List<Long> ids = XTEntityUtils.getIdList(list);
			List<RemoveVO> voList = new ArrayList<RemoveVO>();
			Map<Long, FileEO> files = fileService.getMapTopOne(ids, RemoveEO.class.getName(), null);
			for (RemoveEO policy : list) {
				RemoveVO d = new RemoveVO();
				BeanUtils.copyProperties(policy, d);
				voList.add(d);
				FileEO file = files.get(d.getId());
				if (file != null) {
					d.setFileId(file.getId());
				}
			}
			result.setDataList(voList);
		}
		return result;
	}

	@Override
	public RemoveVO getInfo(Long id) throws XTBusinessException {
		RemoveEO remove = super.getByPrimaryKey(id);
		if (remove != null) {
			RemoveVO vo = new RemoveVO();
			BeanUtils.copyProperties(remove, vo);
			List<FileEO> files = fileService.getFileById(id, RemoveEO.class.getName(), null);
			if (!XTListUtils.isEmpty(files)) {
				vo.setFileAction(FileUrlAdapter.assemblyAction(files.get(0).getId()));
				vo.setFileIds(XTEntityUtils.getIdList(files));
			}
			List<RemoveScheduleEO>  list = removeScheduleMapper.selectByPrimaryRemoveId(id);
			vo.setScheduleList(list);
			return vo;
		}
		return null;
	}

}
